package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SportDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Sport;
import com.itheima.service.SportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = SportService.class)
@Transactional
public class SportServiceImpl implements SportService {

    @Autowired
    private SportDao sportDao;


    /**
     * 批量新增运动信息
     *
     * @param data
     */
    public void insertSport(List<Sport> data) {
        for (Sport sport : data) {
            sportDao.insertSport(sport);
        }
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
//        获取查询条件
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Sport> page = sportDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 添加运动库信息
     *
     * @param sport
     */
    public void addSport(Sport sport) {
        sportDao.addSport(sport);
    }


    /**
     * 根据id查询数据库信息
     *
     * @param id
     * @return
     */
    public Sport findSportById(Integer id) {
        return sportDao.findSportById(id);
    }

    /**
     * 根据id删除运动信息
     *
     * @param id
     */
    public void deleteSportById(Integer id) {
        sportDao.deleteSportById(id);
    }


    /**
     * 编辑运动信息
     *
     * @param sport
     */
    public void editSport(Sport sport) {
        sportDao.editSport(sport);
    }


}
